package com.jlf.controller.engine.entity.criterion;

import com.jlf.core.engine.constant.SchemaConstant;
import com.jlf.core.engine.domain.SchemaCriterionDO;
import com.jlf.core.engine.domain.SchemaFieldDO;
import com.jlf.tool.toolkit.BitToolkit;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

/**
 * 模型检索条件的输入参数
 *
 * @author wujr
 * 2023/7/21
 * @version 1.0
 * 【变更历史】
 * [wujr 2023/7/21 1.0 新增]
 */
@Getter
@Setter
public class SchemaCriterionInput {
    /**
     * 查询条件的编码
     */
    @NotBlank(message = "检索条件字段编码不能为空")
    private String criteriaCode;
    /**
     * 该查询字段对应的模型字段 {@link SchemaFieldDO#getClassFieldCode()}
     * 如果该查询条件需要支持多个字段，则多个字段用逗号分隔，字段之间用or关联
     */
    @NotNull(message = "检索条件对应的模型字段不能为空")
    private List<String> classFieldCode;
    /**
     * 是否支持eq查询，默认：true
     */
    private Boolean supportEqual = true;
    /**
     * 是否支持ne查询，默认：false
     */
    private Boolean supportNotEqual = false;
    /**
     * 是否支持大于等于，默认：false
     */
    private Boolean supportGreatEqual = false;
    /**
     * 是否支持大于，默认：false
     */
    private Boolean supportGreatThan = false;
    private Boolean supportLessThan = false;
    private Boolean supportLessEqual = false;
    /**
     * 是否支持like查询，仅针对varchar的字段有效，默认：false，对于varchar字段，默认：true
     */
    private Boolean supportLike = false;
    /**
     * 是否支持notLike查询，仅针对varchar的字段有效，默认：false
     */
    private Boolean supportNotLike = false;
    private Boolean supportLikeLeft = false;
    private Boolean supportLikeRight = false;
    private Boolean supportNotLikeLeft = false;
    private Boolean supportNotLikeRight = false;

    public SchemaCriterionDO to(){
        SchemaCriterionDO record = new SchemaCriterionDO();
        record.setCriterionCode(criteriaCode);
        int supportCriteria = 0;
        if (Boolean.TRUE.equals(supportEqual)) {
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.eq - 1);
        }
        if (Boolean.TRUE.equals(supportNotEqual)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.ne - 1);
        }
        if (Boolean.TRUE.equals(supportLike)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.like - 1);
        }
        if (Boolean.TRUE.equals(supportNotLike)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.notLike - 1);
        }
        if (Boolean.TRUE.equals(supportLikeLeft)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.likeLeft - 1);
        }
        if (Boolean.TRUE.equals(supportLikeRight)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.likeRight - 1);
        }
        if (Boolean.TRUE.equals(supportNotLikeLeft)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.notLikeLeft - 1);
        }
        if (Boolean.TRUE.equals(supportNotLikeRight)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.notLikeRight - 1);
        }
        if (Boolean.TRUE.equals(supportGreatEqual)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.ge - 1);
        }
        if (Boolean.TRUE.equals(supportGreatThan)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.gt - 1);
        }
        if (Boolean.TRUE.equals(supportLessEqual)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.le - 1);
        }
        if (Boolean.TRUE.equals(supportLessThan)){
            supportCriteria = BitToolkit.set(supportCriteria, SchemaConstant.Jdbc.Criterion.lt - 1);
        }
        record.setSupportCriterion(supportCriteria);

        return record;
    }
}
